/**
 * Created by mango on 2017/3/3.
 */

let fs = require('fs');
let path = require('path');

let describe = require('./describe');
let method = require('./method');
let urlInfo = require('./urlInfo');
let requestDescribe = require('./request-describe');
let requestExample = require('./request-example');
let responseExample = require('./response-example');
let responseDescribe = require('./response-describe');

var index = {

    createFile: function (buildPath, file, content) {
        if (file.type == "dir") {
            // todo 生成 index.md
            var dirPath = path.join(buildPath, file.path);
            if (!fs.existsSync(dirPath)) {
                fs.mkdirSync(dirPath);
            }

            fs.open(path.join(buildPath, file.path, file.fileName) + '.md', "wx+", (error, fd) => {
                    if (error) {
                        debugger;
                    }
                    fs.writeSync(fd, "todo 生成 index.md", "utf8");
                    fs.closeSync(fd);
                }
            );
        }
        if (file.type == "file") {
            // todo fileName 文件

            let list = [
                describe.create(content),
                method.create(content),
                urlInfo.create(content),
                requestDescribe.create(content),
                requestExample.create(content),
                responseDescribe.create(content),
                responseExample.create(content)
            ];

            var md = path.join(buildPath, file.filePath, file.fileName) + '.md';
            fs.open(md, "wx+", (error, fd) => {
                    if (error) {
                        debugger;
                        console.log(error);
                        return;
                    }
                    fs.writeSync(fd, list.join("\r\n\r\n"), "utf8");
                    fs.closeSync(fd);
                }
            );


        }
    }
}

module.exports = index;





